import sys
sys.path.append('.')
from manimlib import *
from utils.shape import *

C_KEYWORD = '#ffb871'
C_FUNC = '#ffb871'
C_BUILTIN_VAR = '#58e35a'
C_VAR = '#b2b1ff'
C_TMPVAR = '#fff899'
C_DIGIT = '#ff8080'

t2c = {
    'GML': GREEN, 'Rooms': GREY_BROWN,
    'Sprite': YELLOW_E, 'Object': BLUE_D, 'Room': GREY_BROWN, 'Script': GREEN_D, 'Event': GREEN_D,
    'x': C_BUILTIN_VAR, 'y': C_BUILTIN_VAR
}

class IntroTemplate(Scene):
    str1 = '【GMS2】GameMaker Studio 2 零基础入门教程'
    str2 = ''

    def construct(self) -> None:
        txt1 = Text(self.str1, color=GREY_D).scale(0.6)
        txt1[:6].set_fill(BLUE_D)
        txt2 = Text(self.str2, color=GREY).scale(0.7)
        txt = VGroup(txt1, txt2).arrange(DOWN).set_stroke(BLACK)

        self.wait(0.1)
        self.play(DrawBorderThenFill(txt))
        self.wait()
        self.play(FadeOut(txt))

class SubTitleTemplate(Scene):
    text = 'TEST'
    buff = MED_SMALL_BUFF

    def construct(self) -> None:
        txt = Text(self.text, font_size=36).set_color(BLUE_D)
        rect = Rectangle(FRAME_WIDTH, txt.get_height() + 2 * self.buff)
        rect.set_fill(WHITE, 0.85).set_stroke(width=0).shift(LEFT * FRAME_WIDTH)

        self.play(rect.animate.shift(RIGHT * FRAME_WIDTH), Write(txt), rate_func=rush_from, run_time=0.5)
        self.wait(2)
        self.play(rect.animate.shift(RIGHT * FRAME_WIDTH), FadeOut(txt), rate_func=rush_into, run_time=0.5)

class IndicatorTemplate(Scene):
    verts = []
    fill_opacity = 0.1

    def construct(self) -> None:
        for i in range(0, len(self.verts) // 2):
            r = simple_sub(surcamera(0.1), corner2rect(self.verts[i * 2], self.verts[i * 2 + 1]))\
                .set_fill(BLACK, self.fill_opacity).set_stroke(YELLOW, 3, 1, False)
            self.play(FadeIn(r), run_time=0.5)
            self.wait()
            self.play(FadeOut(r), run_time=0.5)
            self.wait()

class GMRoomPlane(VGroup):
    def __init__(self, xlen, ylen, color, **kwargs):
        self.np = NumberPlane((0, xlen), (0, ylen)).stretch(-1, 1).set_color(color)
        self.npOrig = Dot(self.np.c2p(), color = BLACK)
        self.npXArrow = Arrow(self.np.x_axis.get_start(), self.np.x_axis.get_end() + RIGHT * 0.3, buff = 0).set_color(BLACK)
        self.npYArrow = Arrow(self.np.y_axis.get_start(), self.np.y_axis.get_end() + DOWN * 0.3, buff = 0).set_color(BLACK)
        self.npXLabel = Tex('x', color = BLACK).next_to(self.npXArrow)
        self.npYLabel = Tex('y', color = BLACK).next_to(self.npYArrow, DOWN)
        super().__init__(*[self.np, self.npOrig, self.npXArrow, self.npYArrow, self.npXLabel, self.npYLabel], **kwargs)

    def animate1(self):
        return ShowCreation(self.np, lag_ratio = 0.01)
    def animate2Array(self):
        return [FadeIn(self.npOrig, scale_factor = 1.3), 
            GrowArrow(self.npXArrow), GrowArrow(self.npYArrow),
            DrawBorderThenFill(self.npXLabel), DrawBorderThenFill(self.npYLabel)]